package com.ice.work.project.smart.doc.builder;

import com.ice.work.project.smart.doc.constant.DocGlobalConstant;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 生成api文档
 * @Author john
 * @Create 2019-12-30
 * @Since 1.0.0
 */
@Slf4j
@Component
public class BuildDoc extends AbstractBuildDoc {

    @Override
    public Map<String, JavaClass> loadJavaFile() {
        log.debug("开始加载java文件");
        Map<String, JavaClass> javaFilesMap = new ConcurrentHashMap<>(100);
        JavaProjectBuilder builder = new JavaProjectBuilder();
        String path = BuildDoc.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        path = path.substring(1);
        File file = new File(path);
        String javaFilePath = file.getParentFile().getParentFile().getPath() + DocGlobalConstant.PROJECT_CODE_PATH;
        builder.addSourceTree(new File(javaFilePath));
        Collection<JavaClass> javaClasses = builder.getClasses();
        for (JavaClass cls : javaClasses) {
            javaFilesMap.put(cls.getFullyQualifiedName(), cls);
        }
        return javaFilesMap;
    }

}
